<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//商品模型
class Goods_model extends CI_Model{
	const TBL_GOODS = 'goods';
    const TBL_CATE = "category";
    const TBL_ATTR = 'attribute';
    const TBL_GOODS_ATTR = 'goods_attr';


	#获取推荐商品信息
	public function best_goods(){
		$condition['is_best'] = 1;
		$query = $this->db->limit(5)->where($condition)->get(self::TBL_GOODS);
		return $query->result_array();
	}
	//获取热卖商品信息
    public function hot_goods(){
        $condition['is_hot'] = 1;
        $query = $this->db->limit(5)->where($condition)->get(self::TBL_GOODS);
        return $query->result_array();
    }

    //获取点击量高的商品排前面
    public function hit_goods(){
        $query = $this->db->order_by('click_count','DESC')->get(self::TBL_GOODS);
        return $query->result_array();
    }

    #获取商品信息
    public function get_goods($goods_id){
        $condition['goods_id'] = $goods_id;
        $query = $this->db->where($condition)->get(self::TBL_GOODS);
        return $query->row_array();
    }

    #获取商品属性信息
    public function list_attrs($goods_id){
        $this->db->select('attribute.attr_id as attid,attribute.attr_name,attribute.type_id,attribute.attr_input_type,goods_attr.goods_id,goods_attr.attr_id,goods_attr.attr_value');
        $this->db->from(self::TBL_GOODS_ATTR);
        $this->db->join(self::TBL_ATTR,'attribute.attr_id = goods_attr.attr_id','left');
        $this->db->where('attribute.attr_input_type',0);
        $this->db->where('goods_attr.goods_id',$goods_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function list_goods($limit,$offset,$cat_id,$goods_name=NULL){
        $condition['is_show'] = 1;
        $this->db->limit($limit,$offset);
        if(!empty($cat_id)){
            $condition['cat_id'] = $cat_id;
        }
        if(isset($goods_name)){
            $this->db->like('goods_name', $goods_name);
        }

        $query = $this->db->where($condition)->get(self::TBL_GOODS);
        return $query->result_array();
    }

    public function count_goods($cat_id,$goods_name=NULL){
        $condition['is_show'] = 1;
        if(!empty($cat_id)){
            $condition['cat_id'] = $cat_id;
        }
        if(isset($goods_name)){
            $this->db->like('goods_name', $goods_name);
        }
        return $this->db->where($condition)->count_all_results(self::TBL_GOODS);
    }


    #获取商品属性信息
    public function list_cat_goods($cat_id){
        $condition['is_show'] = 1;
        $condition['is_new'] = 1;
        if(!empty($cat_id)){
            $condition['cat_id'] = $cat_id;
        }
        $query = $this->db->where($condition)->get(self::TBL_GOODS);
        return $query->result_array();
    }


    /*
       getCatTree
       pram: int $id
       return $id栏目的子孙树
   */
    public function getCatTree($arr,$id = 0) {
        $tree = array();

        foreach($arr as $v) {
            if($v['parent_id'] == $id) {
                $tree[] = $v;

                $tree = array_merge($tree,$this->getCatTree($arr,$v['cat_id']));
            }
        }

        return $tree;
    }



    public function catGoods($cat_id) {
        $query = $this->db->get(self::TBL_CATE);
        $cats = $query->result_array();// 取出所有的栏目来
        $sons = $this->getCatTree($cats,$cat_id);  // 取出给定栏目的子孙栏目

        $sub = array($cat_id);

        if(!empty($sons)) { // 没有子孙栏目
            foreach($sons as $v) {
                $sub[] = $v['cat_id'];
            }
        }

        $in = implode(',',$sub);

        $sql = "select goods_id,goods_name,shop_price,goods_img,cat_id from  bph_goods where cat_id in ( $in) ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }



}